<template>
  <div>
    <van-nav-bar
      title="我的地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择','请选择','请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
      :address-info="obj"
    />

  </div>
</template>

<script>
  import * as get from '@/api/getProList.js';
  import * as post from '@/api/postProList.js';
  export default {
    name:'addAddr',
    data() {
      return {
        areaList:{
          province_list: {
            110000: '北京市',
            120000: '天津市',
            130000: '河南省'
          },
          city_list: {
            110100: '北京市',
            110200: '县',
            120100: '天津市',
            120200: '县',
            130100: '郑州市',
            130200: '开封市',
            130300: '洛阳市',
            130400: '平顶山市'
          },
          county_list: {
            110101: '东城区',
            110102: '西城区',
            110105: '朝阳区',
            110106: '丰台区',
            120101: '和平区',
            120102: '河东区',
            120103: '河西区',
            120104: '南开区',
            120105: '河北区',
            130101: '二七区',
            130102: '金水区',
            130103: '中原区',
            130104: '高新区',
            130201: '龙亭区',
            130202: '顺和回族区',
            130203: '兰考县'
          }
        },
        searchResult: [],
        obj:{
          id:'',
          name:'',
          tel:'',
          province:'河南省',
          city:'郑州市',
          county:'二七区',
          addressDetail:'',
          isDefault:false
        }
      }
    },

    methods: {
      onSave(content) {
        let msg = content;
        let cz = this.$route.query.from;
        if(cz == 'add'){
          post.addAddr({
            receiver:msg.name,
            mobile:msg.tel,
            regions:msg.province+'/'+msg.city+'/'+msg.county,
            address:msg.addressDetail,
            isDefault:msg.ifDefault
          }).then(res=>{
            if(res.status == 200){
              this.$router.back(-1);
            }else{
            }
          })
        }else if(cz == 'change'){
          let id = this.$route.query.id;
          post.updateAddr({
            id:id,
            receiver:msg.name,
            mobile:msg.tel,
            regions:msg.province+'/'+msg.city+'/'+msg.county,
            address:msg.addressDetail,
            isDefault:msg.ifDefault
          }).then(res=>{
            if(res.status == 200){
              this.$router.back(-1);
            }
          })
        }

      },
      onDelete() {
        Toast('delete');
      },
      onChangeDetail(val) {
        if (val) {
        } else {
          this.searchResult = [];
        }
      },
      onClickLeft(){
        this.$router.back(-1)
      }
    },
    mounted(){
      let query = this.$route.query;
      if(query.from == 'change'){
        let id = query.id;
        get.getAddrId({
          id
        }).then(res=>{
          if(res.status == 200){
            let msg = res.data;
            this.obj.id = msg._id;
            this.obj.name = msg.receiver;
            this.obj.tel = msg.mobile;
            this.obj.province = msg.regions.split('/')[0];
            this.obj.city = msg.regions.split('/')[1];
            this.obj.county = msg.regions.split('/')[2];
            this.obj.addressDetail = msg.address;
          }
        })
      }
    }
  }
</script>

<style>
</style>
